<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button
          type="primary"
          danger
          preIcon="ant-design:delete-outlined"
          @click="handleDeleteTable"
        >
          删除
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '查看',
              icon: 'ant-design:search-outlined',
              onClick: handleInfo.bind(null, record),
            },
            {
              label: '删除',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              onClick: handleDelete.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <LogDrawer @register="registerDrawer" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getLogList, LogDel } from '/@/api/demo/auth';

  import { columns, searchFormSchema } from './log.data';

  import { useDrawer } from '/@/components/Drawer';
  import { useMessage } from '/@/hooks/web/useMessage';
  import LogDrawer from './LogDrawer.vue';

  export default defineComponent({
    name: 'AdminLog',
    components: { BasicTable, LogDrawer, TableAction },
    setup() {
      const { createMessage, createConfirm } = useMessage();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const { warning } = createMessage;
      const [registerTable, { reload, getSelectRowKeys }] = useTable({
        title: '日志列表',
        api: getLogList,
        rowKey: 'id',
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
          autoSubmitOnEnter: true,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        rowSelection: {
          type: 'checkbox',
        },
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
      });

      function handleInfo(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      function handleDelete(record: Recordable) {
        console.log(getSelectRowKeys());
        createConfirm({
          iconType: 'warning',
          title: '是否确认删除',
          onOk: function () {
            //调用删除方法
            console.log('ok');
            console.log(record);
            var ids = [record.id];
            LogDel(ids).then(() => {
              reload();
            });
          },
          onCancel: function () {
            console.log('cancel');
          },
        });
      }

      function handleDeleteTable() {
        console.log(getSelectRowKeys());
        var ids = getSelectRowKeys();
        if (ids.length == 0) {
          warning('请选择要删除的行');
          return false;
        }
        createConfirm({
          iconType: 'warning',
          title: '是否确认删除',
          onOk: function () {
            //调用删除方法
            console.log('ok');
            LogDel(ids).then(() => {
              reload();
            });
          },
          onCancel: function () {
            console.log('cancel');
          },
        });
      }

      return {
        registerTable,
        registerDrawer,
        handleInfo,
        handleDelete,
        handleDeleteTable,
      };
    },
  });
</script>
